package org.yang.servlet;/**
 * @author yang
 * @date 2024-04-25
 */

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.yang.date.LocalDateTimeAdapter;
import org.yang.entity.User;
import org.yang.service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.List;

@WebServlet("/GetAllUserServlet")
public class GetAllUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        List<User> userArr = new UserService().findAllUser();

        Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()).create();
        PrintWriter out = resp.getWriter();
        out.print(gson.toJson(userArr));
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
